//
//  LoginViewController.swift
//  Here_Swift
//
//  Created by 童煊 on 2017/12/25.
//  Copyright © 2017年 童煊. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {
    
    /** 欢迎label*/
    var welcomeLabel = UILabel.init()
    /** 手机号输入框*/
    var phoneTextField = UITextField.init()
    /** 手机号分割线*/
    var separatorLine_phone = UILabel.init()
    /** 密码输入框*/
    var pwdTextField = UITextField.init()
    /** 密码分割线*/
    var separatorLine_pwd = UILabel.init()
    /** 登录按钮*/
    var loginBtn = UIButton.init(type: UIButtonType.custom)
    /** 注册按钮*/
    var registBtn = UIButton.init(type: UIButtonType.custom)
    /** 找回密码按钮*/
    var forgetBtn = UIButton.init(type: UIButtonType.custom)
    /** 第三方登录_微博*/
    var weiboLogin = UIButton.init(type: UIButtonType.custom)
    /** 第三方登录_微信*/
    var wechatLogin = UIButton.init(type: UIButtonType.custom)
    
    var info:String?
    
    convenience init(info:String){
        self.init()
        self.info = info
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initView()
        self.loadSignal()
    }

    func initView() -> Void {
        self.view.backgroundColor = UIColor.white
        
        welcomeLabel.text = "欢迎来到Here"
        welcomeLabel.font = UIFont.systemFont(ofSize: 24)
        welcomeLabel.textColor = UIColorFromRGB(rgbValue: 0x3d3d3d)
        welcomeLabel.sizeToFit()
        self.view.addSubview(welcomeLabel)
        welcomeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(40)
            make.top.equalTo(self.view).offset(98)
        }
        
        phoneTextField.placeholder = "请输入手机号"
        phoneTextField.setValue(UIColorFromRGB(rgbValue: 0xcfcfcf), forKeyPath: "_placeholderLabel.textColor")
        phoneTextField.setValue(UIFont.systemFont(ofSize: 16), forKeyPath: "_placeholderLabel.font")
        phoneTextField.textColor = UIColorFromRGB(rgbValue: 0x3d3d3d)
        phoneTextField.font = UIFont.systemFont(ofSize: 16)
        phoneTextField.keyboardType = UIKeyboardType.numberPad
        phoneTextField.delegate = self
        self.view.addSubview(phoneTextField)
        phoneTextField.snp.makeConstraints { (make) in
            make.left.equalTo(self.welcomeLabel)
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.welcomeLabel.snp.bottom).offset(50)
            make.height.equalTo(29)
        }
        
        separatorLine_phone.backgroundColor = UIColorFromRGB(rgbValue: 0xe6e6e6)
        self.view.addSubview(separatorLine_phone)
        separatorLine_phone.snp.makeConstraints { (make) in
            make.top.equalTo(self.phoneTextField.snp.bottom)
            make.centerX.width.equalTo(self.phoneTextField)
            make.height.equalTo(1)
        }
        
        pwdTextField.placeholder = "请输入密码"
        pwdTextField.setValue(UIColorFromRGB(rgbValue: 0xcfcfcf), forKeyPath: "_placeholderLabel.textColor")
        pwdTextField.setValue(UIFont.systemFont(ofSize: 16), forKeyPath: "_placeholderLabel.font")
        pwdTextField.textColor = UIColorFromRGB(rgbValue: 0x3d3d3d)
        pwdTextField.isSecureTextEntry = true
        pwdTextField.font = UIFont.systemFont(ofSize: 16)
        pwdTextField.keyboardType = UIKeyboardType.default
        pwdTextField.delegate = self
        self.view.addSubview(pwdTextField)
        pwdTextField.snp.makeConstraints { (make) in
            make.centerX.left.equalTo(self.phoneTextField)
            make.top.equalTo(self.phoneTextField.snp.bottom).offset(36)
            make.height.equalTo(29)
        }
        
        separatorLine_pwd.backgroundColor = UIColorFromRGB(rgbValue: 0xe6e6e6)
        self.view.addSubview(separatorLine_pwd)
        separatorLine_pwd.snp.makeConstraints { (make) in
            make.top.equalTo(self.pwdTextField.snp.bottom)
            make.centerX.width.equalTo(self.pwdTextField)
            make.height.equalTo(1)
        }
        
        loginBtn.backgroundColor = kAppBaseDesignColor
        loginBtn.setTitle("登录", for: UIControlState.normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        viewRadius(view: loginBtn,radius: 5)
        self.view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { (make) in
            make.centerX.width.equalTo(self.pwdTextField)
            make.top.equalTo(self.pwdTextField.snp.bottom).offset(21)
            make.height.equalTo(44)
        }
        
        registBtn.setTitle("立即注册", for: UIControlState.normal)
        registBtn.setTitleColor(UIColorFromRGB(rgbValue: 0xcfcfcf), for: UIControlState.normal)
        registBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.view.addSubview(registBtn)
        registBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.loginBtn)
            make.top.equalTo(self.loginBtn.snp.bottom).offset(15)
            make.height.equalTo(17)
        }
        
        forgetBtn.setTitle("找回密码", for: UIControlState.normal)
        forgetBtn.setTitleColor(UIColorFromRGB(rgbValue: 0xcfcfcf), for: UIControlState.normal)
        forgetBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.view.addSubview(forgetBtn)
        forgetBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self.loginBtn)
            make.top.equalTo(self.loginBtn.snp.bottom).offset(15)
            make.height.equalTo(17)
        }
        
        weiboLogin.setImage(#imageLiteral(resourceName: "weibo"), for: UIControlState.normal)
        self.view.addSubview(weiboLogin)
        weiboLogin.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(130)
            make.bottom.equalTo(self.view).offset(-49)
            make.height.width.equalTo(36)
        }
        
        wechatLogin.setImage(#imageLiteral(resourceName: "wechat"), for: UIControlState.normal)
        self.view.addSubview(wechatLogin)
        wechatLogin.snp.makeConstraints { (make) in
            make.right.equalTo(self.view).offset(-130)
            make.bottom.equalTo(self.view).offset(-49)
            make.height.width.equalTo(36)
        }
        
    }
    
    func loadSignal() -> Void {
        let tapGes = UITapGestureRecognizer.init()
        tapGes.reactive.stateChanged.observe { (tap) in
            self.view.endEditing(true)
        }
        self.view.addGestureRecognizer(tapGes)
        
        self.loginBtn.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("点击登录")
            let appDelegate = (UIApplication.shared.delegate) as! AppDelegate
            appDelegate.switchRootVC(isLogin: true)
        }
        
        self.registBtn.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("点击注册")
            TXNetworkManager.shareManager().networkRequest(path: "phones/18502358195/code", params: nil, requestType: .PUT, successClosure: { (data, error) in
                
            },failureClosure:{ (data, error, fRModel) in
                
            })
        }
        
        self.forgetBtn.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("找回密码")
            OAuth2Handler().refreshOAuthToken(success: { (data, error) in
                
            }, failure: { (data, error, fRModel) in
                
            })
        }
        
        self.weiboLogin.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("微博登录")
            OAuth2Handler().getOAuthToken(username: "18502358195", password: "123456", success: { (data, error) in
                
            }, failure: { (data, error, fRModel) in
                
            })
        }
        
        self.wechatLogin.reactive.controlEvents(UIControlEvents.touchUpInside).observe { (touch) in
            print("微信登录")
            TXNetworkManager.shareManager().networkRequest(path: "timeline", params: Dictionary(), requestType: .GET, successClosure: { (data, error) in
                
            }, failureClosure: { (data, error, fRModel) in
                
            })
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension LoginViewController:UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        //字符串长度超过限定值后，输入停止
        if textField === self.phoneTextField {//电话号码不超过11位
            if string.count==0{
                return true
            }
            let existedCount = textField.text?.count
            let selectedLength = range.length
            let replaceCount = string.count
            if existedCount! - selectedLength + replaceCount > 11{
                return false
            }
        }else{
            if string.count==0{//密码不超过16位
                return true
            }
            let existedCount = textField.text?.count
            let selectedLength = range.length
            let replaceCount = string.count
            if existedCount! - selectedLength + replaceCount > 16{
                return false
            }
        }
        return true
    }
}
